package com.rtwo.beauty;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

/**
 * rtwo 创建于 2016/4/9 14:36
 */
public class RemoveActivity extends Activity {

    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        position = getIntent().getIntExtra("position",-1);
        setContentView(new MyView(this));
    }

    class MyView extends View{

        private int mWidth;
        private int mHeight;
        private Bitmap beforeBitmap;
        private Bitmap bitmap;
        private Canvas mcanvas;
        private Paint paint;
        private float x;
        private float y;
        private int drawId;

        public MyView(Context context) {
            super(context);
            setFocusable(true);
            setWindow();
            init();
            setBackground();
            try {
                drawId = R.drawable.class.getDeclaredField("g"+(position+1)+"_up").getInt(this);
                beforeBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), drawId), mWidth, mHeight, true);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            setBeforeImage(beforeBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(bitmap, 0, 0, null);
            mcanvas.drawCircle(x, y, 20, paint);
            super.onDraw(canvas);
        }

        private void setWindow() {
            Display display = getWindowManager().getDefaultDisplay();
            this.mWidth = display.getWidth();
            this.mHeight = display.getHeight();
        }

        private void init(){
            mcanvas = new Canvas();
            paint = new Paint();
        }

        private void setBackground(){
            try {
                int drawableId = R.drawable.class.getDeclaredField("g"+(position+1)+"_back").getInt(this);
                this.setBackgroundResource(drawableId);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

        }

        private void setBeforeImage(Bitmap beforeBitmap2) {
            //设置画笔
            paint.setAlpha(0);
//            paint.setColor(Color.RED);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//取两层绘制交集显示上层
            paint.setAntiAlias(true);//用来防止边缘的锯齿

            bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);//生成一个透明的图片
            mcanvas.setBitmap(bitmap);//将透明图片添加到画布
            mcanvas.drawBitmap(beforeBitmap2, 0, 0, null);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            x = event.getX();
            y = event.getY();
            this.invalidate();
            return true;
        }
    }
}
